#include "EconCore.h"
#include <numeric>
#include <cmath>

// GDP calculation (Y = C + I + G + NX)
double calculateGDP(double C, double I, double G, double NX) {
    return C + I + G + NX;
}

// Inflation rate calculation
double calculateInflationRate(double currentCPI, double previousCPI) {
    return ((currentCPI - previousCPI) / previousCPI) * 100;
}

// Linear regression (least squares)
void linearRegression(const DataPoints* data, double* slope, double* intercept) {
    double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
    const size_t n = data->count;

    for (size_t i = 0; i < n; ++i) {
        sumX += data->x[i];
        sumY += data->y[i];
        sumXY += data->x[i] * data->y[i];
        sumX2 += data->x[i] * data->x[i];
    }

    *slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
    *intercept = (sumY - *slope * sumX) / n;
}

// Moving average calculation
double calculateMovingAverage(const TimeSeries* series, int window) {
    if (window <= 0 || window > series->size) return NAN;
    
    double sum = 0;
    for (int i = series->size - window; i < series->size; ++i) {
        sum += series->values[i];
    }
    return sum / window;
}

// Memory management
void freeTimeSeries(TimeSeries* series) {
    delete[] series->values;
    delete series;
}

void freeDataPoints(DataPoints* points) {
    delete[] points->x;
    delete[] points->y;
    delete points;
}